﻿using Alos.Entity;
using Alos.IRepository;
using PetaPoco;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Alos.Repository
{
    public class ReResourceRepository : BaseRepository<ReResource>, IReResourceRepository
    {

        public IEnumerable<dynamic> GetReResourceById(string ids)
        {
            if (string.IsNullOrWhiteSpace(ids))
                return null;
            var sql = Sql.Builder.Append(@"SELECT A.*,ISNULL(C.QtypeName,'')QtypeName FROM [dbo].[ReResource] A left join ReTest c on c.ResourceID=A.ID WHERE A.ID IN (" + ids + ") ");
            return DBContext.Query<dynamic>(sql);

        }

        public IEnumerable<dynamic> GetReResourceByStepId(string ids)
        {
            if (string.IsNullOrWhiteSpace(ids))
                return null;
            var sql = Sql.Builder.Append(@"select A.LessonID,A.StepID, A.ResourceID,ISNULL(C.QtypeName,'') QtypeName, case when (ReOrginName is null or ReOrginName = '') then
 (
select top 1 case E.StemContent when '' THEN D.QtypeName ELSE E.StemContent END StemContent from ReTest D left join ReTestStem E on D.ID=E.TestID where D.ResourceID=B.ID
) else ReOrginName end ReOrginName,B.ResourceFormat,B.ReTypeCode,B.ResourceName,ISNULL( B.TechType,0) TechType from ReLessonResourceNorm A
                            left join ReResource B on A.ResourceID = B.ID 
                            left join ReTest c on c.ResourceID=B.ID
                            where A.StepID in(" + ids + ") order by A.StepID, B.ResourceFormat ");
            return DBContext.Query<dynamic>(sql);

        }

        public IEnumerable<dynamic> GetReResourceByFatherID(int id)
        {
            var sql = Sql.Builder.Append(@"SELECT A.* FROM [dbo].[ReResource] A  WHERE A.FatherID=@0 ", id);
            return DBContext.Query<dynamic>(sql);
        }
        /// <summary>
        /// 根据资源编号获取资源
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ReResource GetReResourceByReResourceId(int id)
        {
            var sql = Sql.Builder.Append(@"SELECT * FROM [dbo].[ReResource]  WHERE ID =@0",id);
            return DBContext.SingleOrDefault<ReResource>(sql);
        }
    }
}
